// pages/mallModule/order/order/order.js
const orderService = require('../../../../api/orderService.js')
const advanceSellService = require('../../../../api/advanceSellService.js')
const teamBuyService = require('../../../../api/teamBuyService.js')
const util = require('../../../../utils/utils.js')
const app = getApp()

Page({
  data: {
    tabType: ["全部", "待付款", "配送中", "待自提", "待评价"],
    tabSelected: '0',
    allOrders: [],
    unpayedOrders: [],
    ungroupOrders: [],
    untakeOrders: [],
    ungetOrders: [],
    unPayedTailOrders: [],
    unEvaluateOrders: [],
    swiperHeight: 0,
    serviceTel: '',
    type: 'normal',
    pageIndex: 1,
    pageSize: 10,
    allOrdersList: [],
    loading: false,
    refresh: false,
    homeBack: false
  },
  // 检查当前用户是否登录授权
  checkAuth: function () {
    try {
      if (!app.globalData.userInfo) {
        return false;
      } else {
        return true;
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  // 获取缓存的用户信息
  getMemberUserInfo: function () {
    if (!app.globalData.userInfo) {
      wx.showToast({
        title: "您还未授权，请登陆授权",
        icon: 'none',
        duration: 2000
      })
    } else {

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    wx.hideShareMenu()
    this.setData({
      serviceTel: app.globalData.servicePhone
    })
    this.getMemberUserInfo()
    console.log(options)
    if (options.type == 'payedTail') {
      wx.setNavigationBarTitle({
        title: '待付尾款'
      })
      this.setData({
        type: 'payedTail'
      })
    }
    if (options.selected) {
      this.setData({
        tabSelected: options.selected
      })
    }
    var pageStack = getCurrentPages()
    if (pageStack.length === 1) {
      this.setData({
        homeBack: true
      })
    }
  },

  //生命周期函数-监听页面初次渲染完毕
  onReady: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        that.setData({
          swiperHeight: res.windowHeight + 10
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    const login = that.checkAuth()
    if (login) {
      if (app.globalData.userInfo.member) {
        if (this.data.type === 'payedTail') {
          console.log("123")
          this.getWaitPayedTail()
        } else {
          this.getOrderData(that.data.tabSelected)
        }
      } else {
        wx.showToast({
          title: '您还不是会员，请先绑定手机号码成为会员',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      console.log("56")
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    const that = this
    const goods = that.data.allOrders
    let imgUrl = null
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
      var teamId = res.target.dataset.teamid
      var teamLeaderRecordId = res.target.dataset.teamleaderrecordid
      var orderId = res.target.dataset.id
      if (goods.length > 0) {
        goods.forEach(item => {
          if (item.orderNum === orderId) {
            if (item.details.length > 0) {
              imgUrl = item.details[0].img
            }
          }
        })
      }
      if (app.globalData.userInfo && app.globalData.userInfo.member) {
        let memberId = app.globalData.userInfo.member.id
        return {
          title: '拼团有优惠',
          path: '/pages/mallModule/fightGroup/groupPurchase/groupPurchase?teamBuyId=' + teamId + '&teamLeaderRecordId=' + teamLeaderRecordId + '&mobile=' + memberId,
          imageUrl: imgUrl,
          success(e) {
            wx.showShareMenu({
              withShareTicket: true
            })
          }
        }
      } else {
        return {
          title: '无界门店商城让生活更便利',
          path: '/pages/mallModule/index/index/index',
          imageUrl: imgUrl,
          success(e) {
            wx.showShareMenu({
              withShareTicket: true
            })
          }
        }
      }
    } else {
      return {
        title: '无界门店商城让生活更便利',
        path: '/pages/mallModule/index/index/index',
        imageUrl: imgUrl,
        success(e) {
          wx.showShareMenu({
            withShareTicket: true
          })
        }
      }
    }
  },
  //切换顶部标签
  switchTab: function (e) {
    console.log(e)
    this.setData({
      tabSelected: e.currentTarget.dataset.idx
    });
    //如果需要加载数据
    const login = this.checkAuth()
    if (login) {
      this.getOrderData(e.currentTarget.dataset.idx)
    }
  },
  //swiperChange
  bindChange: function (e) {
    var that = this;
    this.setData({
      tabSelected: e.detail.current
    });
    //如果需要加载数据
    const login = that.checkAuth()
    if (login) {
      this.getOrderData(e.detail.current)
    }
  },
  getOrderList: function () {
    let allOrders = []
    let allOrdersList = {
      allOrders: [],
      unpayedOrders: [],
      ungroupOrders: [],
      untakeOrders: [],
      ungetOrders: [],
    }
    let postData = {
      page: 1,
      pageSize: 0,
    }
    orderService.query(postData).then(res => {
      console.log(res)
      res.records.forEach((item) => {
        let itemStatus = item.status;
        let itemShipmentType = item.shipmentType

        var tempItem = {
          type: '',
          orderNum: item.id,
          business: item.business,
          status: this.handleOrderStatus(item.status),
          storeName: item.storeName, //发货门店名称
          storeId: item.storeId,
          shipmentAmount: item.shipmentAmount ? item.shipmentAmount : 0, //运费
          details: [],
          buyNumber: item.buyNumber ? item.buyNumber : 0,
          totalPrice: item.cashTotal,
          teamId: item.teamId,
          teamLeaderRecordId: item.teamLeaderRecordId
        }
        if (item.images) {
          item.images.forEach((i) => {
            tempItem.count += parseInt(i.goodsNum);
            tempItem.details.push({
              id: i.productId,
              img: i.url,
              goodsName: i.goodsName,
              goodsNum: i.productNum,
            });
          })
        } else {
          tempItem.count = 0
        }

        if (item.teamId != '' && item.teamId != null) {
          tempItem.type = "拼团"
        } else {
          tempItem.type = ""
        }
        if (item.canRefund === false && itemStatus !== 'REJECTED') {
          tempItem.status = "退款中"
        }
        if (itemStatus === "CREATED") {
          //待付款
          allOrdersList.unpayedOrders.push(tempItem)
        } else if (itemStatus === "PAID" && item.teamId != '' && item.teamId != null && item.canRefund === true) {
          //待拼团
          tempItem.status = "待拼团"
          allOrdersList.ungroupOrders.push(tempItem)
        } else if (itemStatus === "PENDING" && item.canRefund === true) {
          //待自提
          allOrdersList.untakeOrders.push(tempItem)
        } else if (itemStatus === "SHIPPED" && item.canRefund === true) {
          //配送中
          allOrdersList.ungetOrders.push(tempItem)
        }
        allOrdersList.allOrders.push(tempItem)
      })
      this.setData({
        allOrders: allOrdersList.allOrders,
        unpayedOrders: allOrdersList.unpayedOrders,
        ungroupOrders: allOrdersList.ungroupOrders,
        untakeOrders: allOrdersList.untakeOrders,
        ungetOrders: allOrdersList.ungetOrders,
      })
      console.log(allOrdersList)
    })
  },
  getOrderData: function (tabSelected) {
    const that = this
    that.setData({
      pageIndex: 1,
      loading: false
    })
    let tempData = that.queryOrderList(1, 10)
    Promise.all([tempData]).then(res => {
      console.log(res[0])
      const tabNum = tabSelected
      if (tabNum == '0') {
        that.setData({
          allOrders: res[0]
        })
      } else if (tabNum == '1') {
        that.setData({
          unpayedOrders: res[0]
        })
      } else if (tabNum == '2') {
        that.setData({
          ungetOrders: res[0]
        })
      } else if (tabNum == '3') {
        that.setData({
          untakeOrders: res[0]
        })
      } else if (tabNum == '4') {
        that.setData({
          unEvaluateOrders: res[0]
        })
      }
    })
  },
  queryOrderList: function (page, pageSize) {
    const that = this
    const tabNum = that.data.tabSelected
    let status = ''
    if (tabNum == '0') {
      status = ''
    } else if (tabNum == '1') {
      status = 'CREATED'
    } else if (tabNum == '2') {
      status = 'SHIPPED'
    } else if (tabNum == '3') {
      status = 'PENDING'
    } else if (tabNum == '4') {
      status = 'RECEIVED'
    }
    const orderStatus = status
    let postData = {
      page: page,
      pageSize: pageSize,
      orderStatusEquals: status,
    }
    let tempList = []
    if (app.globalData.userInfo && app.globalData.userInfo.member) {
      tempList = orderService.query(postData).then(res => {
        console.log(res)
        let tempOrderList = []
        res.records.forEach((item) => {
          if (item.business !== 'SCORE') {
            let itemStatus = item.status;
            var tempItem = {
              type: '',
              orderNum: item.id,
              status: this.handleOrderStatus(item.status),
              business: item.business,
              storeName: item.storeName, //发货门店名称
              storeId: item.storeId,
              shipmentAmount: item.shipmentAmount ? item.shipmentAmount : 0, //运费
              details: [],
              buyNumber: item.buyNumber ? item.buyNumber : 0,
              totalPrice: item.cashTotal,
              teamId: item.teamId,
              teamLeaderRecordId: item.teamLeaderRecordId
            }
            if (item.images) {
              item.images.forEach((i) => {
                tempItem.count += parseInt(i.goodsNum);
                tempItem.details.push({
                  id: i.productId,
                  img: i.url,
                  goodsName: i.goodsName,
                  goodsNum: i.productNum,
                });
              })
            } else {
              tempItem.count = 0
            }
            if (item.advanceId) {
              tempItem = {
                ...tempItem,
                advanceId: item.advanceId
              }
            }
            if (item.teamId != '' && item.teamId != null) {
              tempItem.type = "拼团"
            } else {
              tempItem.type = ""
            }
            if (item.canRefund === false && itemStatus !== 'REJECTED') {
              tempItem.status = "退款中"
            }
            if (orderStatus === '') {
              // 所有订单
              if (itemStatus === "PAID" && item.teamId != '' && item.teamId != null && item.canRefund === true) {
                tempItem.status = "待拼团"
                tempOrderList.push(tempItem)
                teamBuyService.getDetails(item.teamId, item.teamLeaderRecordId).then(res => {
                  console.log(res)
                  const openTeamTime = res.openTeamTime
                  let timeStamp = new Date(openTeamTime.replace(/-/g, '/')).getTime()
                  let newTimeStamp = parseInt(timeStamp + (res.duration * 1000))
                  const now = new Date().getTime()
                  let teamStatus = ''
                  let status = tempItem.status
                  if (res.teamMemberCount === res.joinMemberCount) {
                    teamStatus = 'success' //团购状态正常
                    status = "已成团"
                  } else {
                    if (now > newTimeStamp) {
                      teamStatus = 'overdue' //团购已过期
                      status = "退款中"
                    } else {
                      teamStatus = 'normal' //团购状态正常
                    }
                  }
                  let tempOrderList = that.data.allOrders
                  console.log(tempOrderList)
                  tempOrderList.forEach((ele, i) => {
                    if (ele.teamLeaderRecordId === res.teamLeaderRecordId) {
                      tempOrderList[i].teamStatus = teamStatus
                      tempOrderList[i].status = status
                    }
                  })
                  that.setData({
                    allOrders: tempOrderList
                  })
                })
              } else {
                tempOrderList.push(tempItem)
              }
            } else if (orderStatus === "CREATED") {
              //待付款
              tempOrderList.push(tempItem)
            } else if (orderStatus === "PAID" && item.teamId != '' && item.teamId != null && item.canRefund === true) {
              //待拼团
              tempItem.status = "待拼团"
              tempOrderList.push(tempItem)
            } else if (orderStatus === "PENDING" && item.canRefund === true) {
              //待自提
              tempOrderList.push(tempItem)
            } else if (orderStatus === "SHIPPED" && item.canRefund === true) {
              //待收货
              tempOrderList.push(tempItem)
            } else if (orderStatus === "RECEIVED" && item.canRefund === true) {
              //待评价
              tempOrderList.push(tempItem)
            }
          } else {

          }
        })
        return tempOrderList
      }).catch(e => {
        wx.showToast({
          title: e.message,
          icon: 'none',
          duration: 2000
        })
      })
    } else {

    }
    return tempList
  },

  handleOrderStatus: function (status) {
    if (status === "CREATED") {
      return "待付款"
    } else if (status === "PAID") {
      return "已付款"
    } else if (status === "SHIPPED") {
      return "配送中"
    } else if (status === "RECEIVED") {
      return "待评价"
    } else if (status === "FINISHED") {
      return "已完成"
    } else if (status === "CANCELED") {
      return "已作废"
    } else if (status === "REJECTED") {
      return "已退款"
    } else if (status === "RETURNING") {
      return "退换货中 "
    } else if (status === "PENDING") {
      return "待自提"
    } else if (status === "WAITSHIPPED") {
      return "待发货"
    } else if (status === "WAITPAYBALANCE") {
      return "待付尾款"
    } else {
      return "状态异常"
    }
  },
  toCancel: function (val) {
    var self = this
    console.log(val)
    let orderId = val.currentTarget.dataset.id
    console.log(orderId)
    wx.showModal({
      title: '提示',
      content: '确认要取消该订单？',
      success: function (res) {
        if (res.confirm) {
          orderService.cancel(orderId).then(item => {
            self.getOrderList()
          })
        } else if (res.cancel) {}
      }
    })
  },
  toPay: function (val) {
    var self = this
    console.log(val)
    let orderId = val.currentTarget.dataset.id
    self.toPayMethod(orderId, 'normal')
    // getPaySign()
    function getPaySign() {
      let tempData = {
        orderId: orderId,
        // payMethod: 'SWIFTPASS'
        payMethod: 'WX_MINI_APP'
      }
      orderService.getPaySign(tempData).then(res => {
        //console.log(res)
        var resData = JSON.parse(res);
        var sign = JSON.parse(resData.sign)
        if (tempData.payMethod === 'WX_MINI_APP') {
          wx.requestPayment({
            'timeStamp': sign.timeStamp,
            'nonceStr': sign.nonceStr,
            'package': sign.packageValue,
            'signType': sign.signType,
            'paySign': sign.paySign,
            'success': function (res) {
              let orderId = tempData.orderId
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 2000
              })
              self.getOrderList()
            },
            'fail': function (res) {
              console.log('支付失败')
              console.log(res)
              if (res.errMsg === 'requestPayment:fail cancel') {
                wx.showToast({
                  title: '您取消了订单支付~',
                  icon: 'none',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: res.errMsg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else if (tempData.payMethod === 'SWIFTPASS') {
          wx.requestPayment({
            'timeStamp': sign.timeStamp,
            'nonceStr': sign.nonceStr,
            'package': sign.package,
            'signType': sign.signType,
            'paySign': sign.paySign,
            'success': function (res) {
              let orderId = tempData.orderId
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 2000
              })
              self.getOrderList()
            },
            'fail': function (res) {
              console.log('支付失败')
              console.log(res)
              if (res.errMsg === 'requestPayment:fail cancel') {
                wx.showToast({
                  title: '您取消了订单支付~',
                  icon: 'none',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: res.errMsg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  // 前往支付页面
  toPayMethod: function (orderId, type) {
    wx.navigateTo({
      url: '../../pay/payMethod/payMethod?orderId=' + orderId + '&from=order&type=' + type
    })
  },
  // 支付成功
  toPaymentSuccess: function (orderId) {
    wx.navigateTo({
      url: '../../pay/payment/payment?orderId=' + orderId
    })
  },
  // 支付尾款
  toPayTail: function (val) {
    var self = this
    console.log(val)
    let orderId = val.currentTarget.dataset.id
    let orderList = self.data.unPayedTailOrders
    if (self.data.type === 'normal') {
      orderList = self.data.allOrders
    }
    let tempData = {
      orderId: orderId,
      // payMethod: 'SWIFTPASS'
      payMethod: 'WX_MINI_APP'
    }
    let advanceId = null
    let productId = null
    orderList.forEach(ele => {
      if (ele.orderNum === orderId) {
        console.log(ele)
        advanceId = ele.advanceId
        productId = ele.details[0].id
      }
    })
    advanceSellService.getByIds(advanceId, productId).then(res => {
      console.log(res)
      let now = new Date()
      let nowTimeStamp = new Date(util.formatTime(now)).getTime()
      const startTimeStamp = new Date(res.balanceStartTime.replace(/-/g, '/')).getTime()
      const endTimeStamp = new Date(res.balanceEndTime.replace(/-/g, '/')).getTime()
      const startTime = res.balanceStartTime.split(" ")[0]
      const startArray = startTime.split("-")
      const newStart = startArray[0] + '年' + startArray[1] + '月' + startArray[2] + '日'
      const endTime = res.balanceEndTime.split(" ")[0]
      const endArray = startTime.split("-")
      const newEnd = endArray[0] + '年' + endArray[1] + '月' + endArray[2] + '日'
      if (nowTimeStamp < startTimeStamp) {
        // 未到支付尾款时间
        wx.showToast({
          title: '请于' + newStart + '至' + newEnd + '期间支付尾款',
          icon: 'none',
          duration: 2000
        })
      } else if (startTimeStamp <= nowTimeStamp && nowTimeStamp <= endTimeStamp) {
        // 支付尾款时间段内
        // getPaySign()
        self.toPayMethod(orderId, 'payTail')
      } else {
        // 已过支付尾款时间
        wx.showToast({
          title: '很抱歉，您的订单已经超过支付尾款的最后期限了~',
          icon: 'none',
          duration: 2000
        })
      }

    })

    function getPaySign() {
      advanceSellService.getBalancePaySign(tempData).then(res => {
        console.log(res)
        var resData = JSON.parse(res);
        var sign = JSON.parse(resData.sign)
        if (tempData.payMethod === 'WX_MINI_APP') {
          wx.requestPayment({
            'timeStamp': sign.timeStamp,
            'nonceStr': sign.nonceStr,
            'package': sign.packageValue,
            'signType': sign.signType,
            'paySign': sign.paySign,
            'success': function (res) {
              let orderId = tempData.orderId
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 2000
              })
              self.toPaymentSuccess(orderId)
            },
            'fail': function (res) {
              console.log('支付失败')
              console.log(res)
              if (res.errMsg === 'requestPayment:fail cancel') {
                wx.showToast({
                  title: '您取消了订单支付~',
                  icon: 'none',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: res.errMsg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else if (tempData.payMethod === 'SWIFTPASS') {
          wx.requestPayment({
            'timeStamp': sign.timeStamp,
            'nonceStr': sign.nonceStr,
            'package': sign.package,
            'signType': sign.signType,
            'paySign': sign.paySign,
            'success': function (res) {
              let orderId = tempData.orderId
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 2000
              })
            },
            'fail': function (res) {
              console.log('支付失败')
              console.log(res)
              if (res.errMsg === 'requestPayment:fail cancel') {
                wx.showToast({
                  title: '您取消了订单支付~',
                  icon: 'none',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: res.errMsg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  toOrderDetails: function (val) {
    console.log(val)
    let orderId = val.currentTarget.dataset.id
    wx.navigateTo({
      url: '../orderDetails/orderDetails?orderId=' + orderId
    })
  },
  handleConfirmReceipt: function (val) {
    var self = this
    console.log(val)
    let orderId = val.currentTarget.dataset.id
    console.log(orderId)
    wx.showModal({
      title: '提示',
      content: '请确认已经收到商品？',
      success: function (res) {
        if (res.confirm) {
          orderService.confirm(orderId).then(item => {
            self.getOrderList()
          })
        } else if (res.cancel) {}
      }
    })
  },
  toRefundGoods: function (val) {
    let orderId = val.currentTarget.dataset.id
    wx.navigateTo({
      url: '../refund/refund?orderId=' + orderId
    })
  },
  toEvaluate: function (val) {
    let orderId = val.currentTarget.dataset.id
    wx.navigateTo({
      url: '../evaluate/evaluate?orderId=' + orderId
    })
  },
  toHome: function () {
    wx.switchTab({
      url: '../../index/index/index'
    })
  },
  toService: function (val) {
    let orderId = val.currentTarget.dataset.id
    const tel = this.data.serviceTel
    wx.showModal({
      title: '提示',
      content: '确认要拨打此电话' + tel + ' ？',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: tel,
          })
        } else if (res.cancel) {}
      }
    })
  },
  // 获取待付尾款订单
  getWaitPayedTail: function () {
    let postData = {
      page: 1,
      pageSize: 0,
      orderStatusEquals: 'WAITPAYBALANCE'
    }
    orderService.query(postData).then(res => {
      console.log(res)
      let tempWaitOrderList = []
      res.records.forEach(item => {
        let itemStatus = item.status;
        var tempItem = {
          type: '',
          orderNum: item.id,
          status: this.handleOrderStatus(item.status),
          storeName: item.storeName, //发货门店名称
          storeId: item.storeId,
          shipmentAmount: item.shipmentAmount ? item.shipmentAmount : 0, //运费
          details: [],
          buyNumber: item.buyNumber ? item.buyNumber : 0,
          totalPrice: item.cashTotal,
          teamId: item.teamId,
          teamLeaderRecordId: item.teamLeaderRecordId,
          advanceId: item.advanceId,
        }
        if (item.images) {
          item.images.forEach((i) => {
            tempItem.count += parseInt(i.goodsNum);
            tempItem.details.push({
              id: i.productId,
              img: i.url,
              goodsName: i.goodsName,
              goodsNum: i.productNum,
            });
          })
        } else {
          tempItem.count = 0
        }
        tempWaitOrderList.push(tempItem)
      })
      this.setData({
        unPayedTailOrders: tempWaitOrderList
      })
    }).catch(e => {
      console.log(e.message)
    })
  },
  // 滚动到顶部
  bindscrolltoupper: function () {
    const that = this
    that.setData({
      refresh: true
    })
    let tempData = this.queryOrderList(1, 10)
    Promise.all([tempData]).then(res => {
      if (res[0].length > 0) {
        setTimeout(() => {
          that.setData({
            allOrders: res[0],
            pageIndex: 1,
            refresh: false
          })
        }, 1000);
      } else {
        wx.showToast({
          title: '已经加载完了~',
          icon: 'none',
          duration: 2000
        })
        that.setData({
          pageIndex: 1,
          refresh: false
        })
      }
    })
  },
  // 滚动到底部
  bindscrolltolower: function () {
    this.loadOrderList()
  },
  loadOrderList: function () {
    const that = this
    const page = that.data.pageIndex
    const pageSize = that.data.pageSize
    const tabNum = that.data.tabSelected
    that.setData({
      loading: true,
      loadingText: '正在加载...'
    })
    let index = page + 1

    let tempData = this.queryOrderList(index, pageSize)
    Promise.all([tempData]).then(res => {
      console.log(that.data.ungroupOrders)
      if (res[0].length > 0) {
        if (tabNum == '0') {
          const allOrders = that.data.allOrders
          console.log(allOrders)
          let tempArray = allOrders.concat(res[0])
          that.setData({
            allOrders: tempArray,
            pageIndex: index,
            loading: false
          })
        } else if (tabNum == '1') {
          const unpayedOrders = that.data.unpayedOrders
          console.log(unpayedOrders)
          let tempArray = unpayedOrders.concat(res[0])
          that.setData({
            unpayedOrders: tempArray,
            pageIndex: index,
            loading: false
          })
        } else if (tabNum == '2') {
          const ungroupOrders = that.data.ungroupOrders
          console.log(ungroupOrders)
          let tempArray = ungroupOrders.concat(res[0])
          that.setData({
            ungroupOrders: tempArray,
            pageIndex: index,
            loading: false
          })
        } else if (tabNum == '3') {
          const untakeOrders = that.data.untakeOrders
          console.log(untakeOrders)
          let tempArray = untakeOrders.concat(res[0])
          that.setData({
            untakeOrders: tempArray,
            pageIndex: index,
            loading: false
          })
        } else if (tabNum == '4') {
          const ungetOrders = that.data.ungetOrders
          console.log(ungetOrders)
          let tempArray = ungetOrders.concat(res[0])
          that.setData({
            ungetOrders: tempArray,
            pageIndex: index,
            loading: false
          })
        }

      } else {
        that.setData({
          pageIndex: page,
          loadingText: '没有更多数据'
          //loading: false
        })
      }
    })
  }
})